package com.taotao.service.impl;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.apitype.ApiHepler;
import com.taotao.apitype.ApiHepler.PagingResult;
import com.taotao.common.pojo.EasyUIDataGridResult;
import com.taotao.dao.TbUserMapper;
import com.taotao.domain.TbUser;
import com.taotao.domain.TbUserExample;
import com.taotao.domain.TbUserExample.Criteria;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;

@Service
public class UserImpl {

	@Autowired
	TbUserMapper userMapper;

	/**
	 * 查询用户列表分页
	 * 
	 * @param page
	 * @param rows
	 * @param username
	 * @return
	 * @throws Exception 
	 */
	public ApiHepler.PagingResponse<TbUser> userList(Map<String, Object> data) throws Exception {
		PagingResult pagingResult = new ApiHepler.PagingResult();
		// 自动赋值分页请求对象
		BeanUtils.populate(pagingResult, data);
		System.out.println("PagingResult cast:" + pagingResult.toString());
		// 分页处理
		PageHelper.startPage(pagingResult.getPage(), pagingResult.getSize());
		// 执行查询
		TbUserExample example = new TbUserExample();
		Criteria createCriteria = example.createCriteria();
		// 查询用户名
		if (data.containsKey("username")) {
			createCriteria = createCriteria.andUsernameLike(String.valueOf(data.get("username")));
		}
		List<TbUser> list = userMapper.selectByExample(example);
		// 取分页信息
		PageInfo<TbUser> pageInfo = new PageInfo<TbUser>(list);
		// 返回处理结果
		return ApiHepler.Paging(pageInfo.getList(), pageInfo.getTotal());
	}
}
